package com.problem.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 解法:
 * 1.两层for循环，暴力解法，时间复杂度O(n2)
 * 2.解法，时间复杂度O(n), 遍历数组，不断缓存当前元素与下标的关系，查看target-num[index]的值是否在map中，如果在即找到了结果，将结果返回
 *
 * @author didi
 * @DESCRIPTION
 * @create 2019-04-15 14:20
 **/

public class Problem1 {
    public int[] twoSum(int[] nums, int target) {

        Map<Integer, Integer> hashmap = new HashMap<>();
        int[]                 result  = new int[2];
        if (nums.length <= 0) {
            return result;
        }
        for (int i = 0; i < nums.length; i++) {
            int anotherValue = target - nums[i];

            if (hashmap.containsKey(anotherValue)) {
                result[0] = hashmap.get(anotherValue);
                result[1] = i;
                return result;
            }
            hashmap.put(nums[i], i);
        }
        return result;
    }

    public static void main(String[] args) {
        Problem1 problem1 = new Problem1();
        int[]    ss       = {3, 3};
        int[]    result   = problem1.twoSum(ss, 6);
        System.out.println(result[0]);
        System.out.println(result[1]);
    }

}
